package com.yu.tool;

import com.yu.DAO.StuInfoDao;
import com.yu.DAO.StudentDao;
import com.yu.POJO.StuInfo;
import com.yu.POJO.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LittleFisher
 * @Date: 2021/06/22/21:01
 * @Description:
 */
public class fff {
    public static void main(String[] args) {
        String resource = "mybatis_config.xml";
        // 得到配置文件流
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建会话工厂，传入mybatis的配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

        // 通过工厂得到SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 传入id删除 用户
      /*  sqlSession.delete("test.deleteUser", 39);*/

        StudentDao sd = sqlSession.getMapper(StudentDao.class);
        StuInfoDao sid = sqlSession.getMapper(StuInfoDao.class);
        int i;
        for(i=1;i<=9;i++){

            Student student = new Student();

            student.setName("陈"+i);
            student.setPassword("123456");
            student.setSno("18005051"+i);
            student.setClazz_id(8);

            StuInfo stuInfo = new StuInfo("21","男","茂名","13725454867","11-324","汉族");

            if(i%2==0){
                stuInfo.setSex("女");
                student.setName("余"+i);
            }
            sd.addStudent(student);
            sid.addStuInfo(stuInfo);
            // 提交事务

        }

        sqlSession.commit();

        // 关闭会话
        sqlSession.close();

    }
}
